package com.cetiti.iot.iotalarm;

import java.util.Random;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class AsyncProducer {
    public static void main(String[] args) throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new DefaultMQProducer("cgi_coap");
        // Specify name server addresses.
        producer.setNamesrvAddr("10.0.40.143:9876");
        
        //Launch the instance.
        producer.start();
        String  topicKeyString = producer.getCreateTopicKey();
        producer.createTopic( MixAll.AUTO_CREATE_TOPIC_KEY_TOPIC, "TopicTest", 2);
        producer.setRetryTimesWhenSendAsyncFailed(0);
        
        for (int i = 0; i < 100; i++) {
                final int index = i;
                //Create a message instance, specifying topic, tag and message body.
                Message msg = new Message("TopicTest",
                    "TagA",
                    "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
                producer.send(msg, new SendCallback() {

                    public void onSuccess(SendResult sendResult) {
                       System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());
                    }
                    public void onException(Throwable e) {
                        System.out.printf("%-10d Exception %s %n", index, e);
                       // e.printStackTrace();
                    }
                });
                
        }
        Thread.sleep(30000);
        //Shut down once the producer instance is not longer in use.
        producer.shutdown();
    }
}
